//
//  UIView+HDJihuoView.m
//  app
//
//  Created by pro on 2017/4/13.
//  Copyright © 2017年 hdaren. All rights reserved.
//

#import "HDJihuoView.h"


@interface HDJihuoView ()<UITextFieldDelegate>



@end

@implementation  HDJihuoView

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        self.userInteractionEnabled = YES;
        [self creatSubViews];
        
    }
    return self;
}

-(void)creatSubViews
{
    UILabel * jihuo_lab = [[UILabel alloc]initWithFrame:CGRectOf([self.ubo fx:0 fy:0], [self.ubo ow:UBO_SCREEN.width oh:50])];
    jihuo_lab.text = @"  未曾激活过?";
    [jihuo_lab setFont:[UIFont systemFontOfSize:14]];
    [jihuo_lab setTextColor:[UIColor whiteColor]];
    jihuo_lab.backgroundColor = [UIColor redColor];
    [jihuo_lab setTextAlignment:NSTextAlignmentLeft];
    [self addSubview:jihuo_lab];
    
    
    UIView * jihuo_view = [[UIView alloc]initWithFrame:CGRectOf([jihuo_lab.ufo fx:0 fy:1 oy:12], [self.ubo ow:UBO_SCREEN.width oh:88])];
    jihuo_view.backgroundColor = [UIColor whiteColor];
    jihuo_view.userInteractionEnabled = YES;
    [self addSubview:jihuo_view];
    
    [self creatJihuoViewSubViews:jihuo_view];
    
    
    UIButton * jihuo_btn = [[UIButton alloc]initWithFrame:CGRectOf([jihuo_view.ufo fx:UB_1_2 ox:-(UBO_SCREEN.width-60)/2 fy:1 oy:12], [self.ubo ow:UBO_SCREEN.width-60 oh:40])];
    jihuo_btn.backgroundColor = [UIColor whiteColor];
    [jihuo_btn setTitle:@"激活" forState:UIControlStateNormal];
    [jihuo_btn setTitleColor:[XCommon hexStringToColor:kColor_red_text] forState:UIControlStateNormal];
    jihuo_btn.layer.borderColor = [XCommon hexStringToColor:kColor_red_text].CGColor;
    jihuo_btn.layer.borderWidth = 0.3;
    [jihuo_btn addTarget:self action:@selector(jihuo:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:jihuo_btn];
    
    
    UILabel * yanzheng_lab = [[UILabel alloc]initWithFrame:CGRectOf([jihuo_btn.ufo fx:0 ox:-30 fy:1 oy:24], [self.ubo ow:UBO_SCREEN.width oh:50])];
    yanzheng_lab.text = @"  已经激活过？";
    [yanzheng_lab setFont:[UIFont systemFontOfSize:14]];
    [yanzheng_lab setTextColor:[UIColor whiteColor]];
    yanzheng_lab.backgroundColor = [UIColor orangeColor];
    [yanzheng_lab setTextAlignment:NSTextAlignmentLeft];
    [self addSubview:yanzheng_lab];

    
    UIView * yanzheng_view = [[UIView alloc]initWithFrame:CGRectOf([yanzheng_lab.ufo fx:0 fy:1 oy:12], [self.ubo ow:UBO_SCREEN.width oh:88])];
    yanzheng_view.userInteractionEnabled = YES;
    yanzheng_view.backgroundColor = [UIColor whiteColor];
    [self addSubview:yanzheng_view];
    
    [self creatYanzhengViewSubviews:yanzheng_view];


    UIButton * yanzheng_btn = [[UIButton alloc]initWithFrame:CGRectOf([yanzheng_view.ufo fx:UB_1_2 ox:-(UBO_SCREEN.width-60)/2 fy:1 oy:12], [self.ubo ow:UBO_SCREEN.width-60 oh:40])];
    yanzheng_btn.backgroundColor = [UIColor whiteColor];
    [yanzheng_btn setTitle:@"重新验证" forState:UIControlStateNormal];
    [yanzheng_btn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    yanzheng_btn.layer.borderColor = [UIColor orangeColor].CGColor;
    yanzheng_btn.layer.borderWidth = 0.3;
    [yanzheng_btn addTarget:self action:@selector(chongxinyanzheng:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:yanzheng_btn];
    
}


-(void)creatJihuoViewSubViews:(UIView*)jihuo_view
{

    self.tf_1 = [[UITextField alloc]initWithFrame:CGRectOf([jihuo_view.ubo fx:0 ox:12 fy:0], [jihuo_view.ubo ow:UBO_SCREEN.width-24 oh:44])];
    self.tf_1.tag = 1;
    self.tf_1.placeholder = @"激活码";
    [self.tf_1 setFont:[UIFont systemFontOfSize:14]];
    [jihuo_view addSubview:self.tf_1];
    
    
    self.tf_2 = [[UITextField alloc]initWithFrame:CGRectOf([self.tf_1.ufo fx:0 fy:1 ], [jihuo_view.ubo ow:UBO_SCREEN.width-24 oh:44])];
    self.tf_2.tag = 2;
    self.tf_2.placeholder = @"手机号";
    [self.tf_2 setFont:[UIFont systemFontOfSize:14]];
    [jihuo_view addSubview:self.tf_2];
    
    
    UIView * top = [[UIView alloc]initWithFrame:CGRectOf([jihuo_view.ubo fx:0 fy:0 py:1], [self.ubo ow:UBO_SCREEN.width ph:1])];
    top.backgroundColor = [XCommon hexStringToColor:kColor_gray_line];
    [jihuo_view addSubview:top];
    
    
    UIView * line = [[UIView alloc]initWithFrame:CGRectOf([jihuo_view.ubo fx:0 fy:UB_1_2 py:1], [self.ubo ow:UBO_SCREEN.width ph:1])];
    line.backgroundColor = [XCommon hexStringToColor:kColor_gray_line];
    [jihuo_view addSubview:line];
    
    
    UIView * bottom = [[UIView alloc]initWithFrame:CGRectOf([jihuo_view.ubo fx:0 fy:1 py:-1], [self.ubo ow:UBO_SCREEN.width ph:1])];
    bottom.backgroundColor = [XCommon hexStringToColor:kColor_gray_line];
    [jihuo_view addSubview:bottom];
    
    
    
}


-(void)creatYanzhengViewSubviews:(UIView*)yanzheng_view
{
    self.tf_3 = [[UITextField alloc]initWithFrame:CGRectOf([yanzheng_view.ubo fx:0 ox:12 fy:0], [yanzheng_view.ubo ow:UBO_SCREEN.width-24 oh:44])];
    self.tf_3.delegate = self;
    self.tf_3.tag = 3;
    self.tf_3.placeholder = @"手机号";
    [self.tf_3 setFont:[UIFont systemFontOfSize:14]];
    [yanzheng_view addSubview:self.tf_3];
    
    
    self.tf_4 = [[UITextField alloc]initWithFrame:CGRectOf([self.tf_3.ufo fx:0 fy:1 ], [yanzheng_view.ubo ow:UBO_SCREEN.width/3*2 oh:44])];
    self.tf_4.delegate = self;
    self.tf_4.tag = 4;
    self.tf_4.placeholder = @"验证码";
    [self.tf_4 setFont:[UIFont systemFontOfSize:14]];
    [yanzheng_view addSubview:self.tf_4];
    
    
    self.send_yanzheng = [[UIButton alloc]initWithFrame:CGRectOf([self.tf_4.ufo fx:1 fy:0 ], [yanzheng_view.ubo ow:UBO_SCREEN.width/3 oh:44])];
    [self.send_yanzheng setTitle:@"获取验证码" forState:UIControlStateNormal];
    [self.send_yanzheng setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [self.send_yanzheng.titleLabel setFont:[UIFont systemFontOfSize:13]];
    [self.send_yanzheng addTarget:self action:@selector(sendcode:) forControlEvents:UIControlEventTouchUpInside];
    [yanzheng_view addSubview:self.send_yanzheng];
    
    
    
    UIView * shu_line = [[UIView alloc]initWithFrame:CGRectOf([self.send_yanzheng.ufo fx:0 px:1 fy:0], [yanzheng_view.ubo pw:1 oh:44])];
    shu_line.backgroundColor = [XCommon hexStringToColor:kColor_gray_line];
    [yanzheng_view addSubview:shu_line];
    
    
    UIView * top = [[UIView alloc]initWithFrame:CGRectOf([yanzheng_view.ubo fx:0 fy:0 py:1], [self.ubo ow:UBO_SCREEN.width ph:1])];
    top.backgroundColor = [XCommon hexStringToColor:kColor_gray_line];
    [yanzheng_view addSubview:top];
    
    UIView * line = [[UIView alloc]initWithFrame:CGRectOf([yanzheng_view.ubo fx:0 fy:UB_1_2 py:1], [self.ubo ow:UBO_SCREEN.width ph:1])];
    line.backgroundColor = [XCommon hexStringToColor:kColor_gray_line];
    [yanzheng_view addSubview:line];
    
    
    UIView * bottom = [[UIView alloc]initWithFrame:CGRectOf([yanzheng_view.ubo fx:0 fy:1 py:-1], [self.ubo ow:UBO_SCREEN.width ph:1])];
    bottom.backgroundColor = [XCommon hexStringToColor:kColor_gray_line];
    [yanzheng_view addSubview:bottom];
    
}


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.tf_1 resignFirstResponder];
    [self.tf_2 resignFirstResponder];
    [self.tf_3 resignFirstResponder];
    [self.tf_4 resignFirstResponder];
    
}

-(void)jihuo:(UIButton*)sender
{
    if (self.tf_1.text.length > 0 && self.tf_2.text.length > 0) {
        [[NSUserDefaults standardUserDefaults]setObject:self.tf_1.text forKey:@"tf_1"];
        [[NSUserDefaults standardUserDefaults]setObject:self.tf_2.text forKey:@"lock_phone"];
        [[NSUserDefaults standardUserDefaults]synchronize];
        
        [self.tf_1 resignFirstResponder];
        [self.tf_2 resignFirstResponder];
        
        
        if (self.delegate && [self.delegate respondsToSelector:@selector(goJIhuo)]) {
            [self.delegate goJIhuo];
        }
        
        
        
    }else
    {
        [MyToast showWithText:@"请将信息填写完整"];
    }
}

-(void)chongxinyanzheng:(UIButton*)sender
{
    if (self.tf_3.text.length > 0 && self.tf_4.text.length > 0) {
        [[NSUserDefaults standardUserDefaults]setObject:self.tf_3.text forKey:@"lock_phone"];
        [[NSUserDefaults standardUserDefaults]setObject:self.tf_4.text forKey:@"tf_4"];
        [[NSUserDefaults standardUserDefaults]synchronize];
        
        [self.tf_3 resignFirstResponder];
        [self.tf_4 resignFirstResponder];
        
        if (self.delegate && [self.delegate respondsToSelector:@selector(goChongXinYanZheng)]) {
            [self.delegate goChongXinYanZheng];
        }
        
        
        
    }else
    {
        [MyToast showWithText:@"请将信息填写完整"];
    }

    
}




-(void)sendcode:(UIButton*)sender
{
    
    if (self.tf_3.text.length == 11) {
        
        [self.tf_3 resignFirstResponder];
        
        sender.enabled = NO;
        
        if (self.delegate && [self.delegate respondsToSelector:@selector(goSendMsg)]) {
            
            [self.delegate goSendMsg];
            
        }
        
    }else
    {
        [MyToast showWithText:@"请输入正确手机号"];
    }
    
}

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (textField == self.tf_3 || textField == self.tf_4) {
        
        if (self.delegate && [self.delegate respondsToSelector:@selector(begainEdit)]) {
            [self.delegate begainEdit];
        }
        
    }
}

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    if (textField == self.tf_3 || textField == self.tf_4) {
        
        if (self.delegate && [self.delegate respondsToSelector:@selector(endEdit)]) {
            [self.delegate endEdit];
        }
        
    }
}


@end
